﻿''' <summary>
''' Represents an MSSQL entry.
''' </summary>
''' <remarks>
''' Objects of this class are used by the MSSQL collector. They represent a query
''' to be run against a specific database.
''' </remarks>
Public Class MSSQLEntry

    ''' <summary>
    ''' MSSQL server name.
    ''' </summary>
    ''' <remarks></remarks>
    Protected m_server As String

    ''' <summary>
    ''' MSSQL server instance.
    ''' </summary>
    ''' <remarks></remarks>
    Protected m_instance As String

    ''' <summary>
    ''' MSSQL server database.
    ''' </summary>
    ''' <remarks></remarks>
    Protected m_database As String

    ''' <summary>
    ''' Determine whether a trusted connection will be used.
    ''' </summary>
    ''' <remarks></remarks>
    Protected m_useIntegratedSecurity As Boolean

    ''' <summary>
    ''' If no trusted connection, this holds the user ID.
    ''' </summary>
    ''' <remarks></remarks>
    Protected m_userID As String

    ''' <summary>
    ''' If no trusted connection, this holds the user password.
    ''' </summary>
    ''' <remarks></remarks>
    Protected m_password As String

    ''' <summary>
    ''' SQL query to run against the database.
    ''' </summary>
    ''' <remarks></remarks>
    Protected m_sqlQuery As String

    ''' <summary>
    ''' Get/set the MSSQL server name.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property Server() As String
        Get
            Return m_server
        End Get
        Set(ByVal value As String)
            m_server = value
        End Set
    End Property

    ''' <summary>
    ''' Get/set the MSSQL server instance.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property Instance() As String
        Get
            Return m_instance
        End Get
        Set(ByVal value As String)
            m_instance = value
        End Set
    End Property

    ''' <summary>
    ''' Get/set the MSSQL database.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property Database() As String
        Get
            Return m_database
        End Get
        Set(ByVal value As String)
            m_database = value
        End Set
    End Property

    ''' <summary>
    ''' Get/set the trusted security flag.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property UseIntegratedSecurity() As Boolean
        Get
            Return m_useIntegratedSecurity
        End Get
        Set(ByVal value As Boolean)
            m_useIntegratedSecurity = value
        End Set
    End Property

    ''' <summary>
    ''' Get/set the user ID.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property UserID() As String
        Get
            Return m_userID
        End Get
        Set(ByVal value As String)
            m_userID = value
        End Set
    End Property

    ''' <summary>
    ''' Get/set the user password.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property Password() As String
        Get
            Return m_password
        End Get
        Set(ByVal value As String)
            m_password = value
        End Set
    End Property

    ''' <summary>
    ''' Get/set the SQL query to execute.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property SQLQuery() As String
        Get
            Return m_sqlQuery
        End Get
        Set(ByVal value As String)
            m_sqlQuery = value
        End Set
    End Property

    ''' <summary>
    ''' Default class constructor.
    ''' </summary>
    ''' <param name="server">MSSQL server name.</param>
    ''' <param name="instance">MSSQL server instance.</param>
    ''' <param name="database">MSSQL server database.</param>
    ''' <param name="useIntegratedSecurity">Trusted security flag.</param>
    ''' <param name="userID">User ID.</param>
    ''' <param name="password">User password.</param>
    ''' <param name="sqlQuery">SQL query to execute.</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal server As String, ByVal instance As String, _
                   ByVal database As String, _
                   ByVal useIntegratedSecurity As Boolean, _
                   ByVal userID As String, ByVal password As String, _
                   ByVal sqlQuery As String)
        m_server = server
        m_instance = instance
        m_database = database
        m_useIntegratedSecurity = useIntegratedSecurity
        m_userID = userID
        m_password = password
        m_sqlQuery = sqlQuery
    End Sub

    ''' <summary>
    ''' Returns an appropriate connection string according to passed parameters.
    ''' </summary>
    ''' <returns>SQL connection string.</returns>
    ''' <remarks></remarks>
    Public Function getConnectionString() As String
        If m_useIntegratedSecurity Then
            Return "Server=" + m_server + getInstance() + ";Database=" + m_database + ";Trusted_Connection=Yes;"
        Else
            Return "Server=" + m_server + getInstance() + ";Database=" + m_database + ";Uid=" + m_userID + ";Pwd=" + m_password + ";"
        End If
    End Function

    ''' <summary>
    ''' If appropriate, returns the instance of MSSQL server to be used.
    ''' </summary>
    ''' <returns>MSSQL server instance.</returns>
    ''' <remarks></remarks>
    Protected Function getInstance() As String
        If IsDBNull(m_instance) OrElse (m_instance Is Nothing) OrElse (m_instance = "") Then
            Return ""
        Else
            Return "\" + m_instance
        End If
    End Function

End Class
